require "lfs"
require "unity"
require "QGMainWindow"

-----------------------------------------------------------------------------
--初始化参数
RadomeDataProc={
	prename_air = "radome-air";
	prename = "radome-total";
	
	--需要导出的源的形式
	src_types = {"SUM","DIFF"};
	
	-- 极化
	polars = {"HH","VV"};

	--需要导出的频率点
	ncols = {1,2,3};
	
	angs = {0,1,2};
	
	names = {};
	
	radome_res = {};
	air_res = {};
	
};
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--创建表和创建元表
function RadomeDataProc:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	return o;
end
-----------------------------------------------------------------------------

function get_bse( str, data, ang, col )
	lable = str[7][col];
	-- ang_data = data[9+ang][2];
	-- ang_rcs = data[9+ang][col];
	lable = str[7][col];
	local ang_arr = std.vector:new();
	local etheta_arr = std.vector:new();
	
	local nangs = #str-9;
	
	for ii=1,nangs do
		ang_arr:push_back( data[8+ii][2] )
		etheta_arr:push_back( data[8+ii][col] )
	end	
	local val=1;
	 idx=0;
	val, idx  = std.min_and_index_with_range( etheta_arr,#str/2*0.6,#str/2 * 1.4);
	min_ang = ang_arr[idx];
	print( val, idx,min_ang )
	 -- local view = mw:create2DView("ang="..ang..","..lable);
	 -- local gp = view:addGraph(ang_arr,etheta_arr)
	  -- view:rescaleAxes()
	return min_ang,val,lable
end

function get_tran( str, data, ang, col )
	lable = str[7][col];
	-- ang_data = data[9+ang][2];
	-- ang_rcs = data[9+ang][col];
	lable = str[7][col];
	local ang_arr = std.vector:new();
	local etheta_arr = std.vector:new();
	
	local nangs = #str-9;
	
	for ii=1,nangs do
		ang_arr:push_back( data[8+ii][2] )
		etheta_arr:push_back( data[8+ii][col] )
	end	
	local val=1;
	 idx=0;
	val, idx  = std.max_and_index_with_range( etheta_arr,#str/2*0.8,#str/2 * 1.2);
	max_ang = ang_arr[idx];
	print( val, idx,max_ang )
	  -- local view = mw:create2DView("ang="..ang..","..lable);
	  -- local gp = view:addGraph(ang_arr,etheta_arr)
	   -- view:rescaleAxes()
	return max_ang,val,lable
end
function RadomeDataProc:read_nor_data()
	self:read_data_from_path(self.prename_air)
	self.air_res = self.read_res;
end
function RadomeDataProc:read_data()
	self:read_data_from_path(self.prename)
	self.radome_res = self.read_res;
end
function RadomeDataProc:read_data_from_path( prename )
	self.read_res = {};
	for _,src_type in pairs (self.src_types) do
		for _,polar in pairs(self.polars) do
			for _,n in pairs(self.ncols) do
				ncol = 3+(n-1)*8;
				if polar == "HH" then
					ncol = 5+(n-1)*8;
				end
				local xm = std.vector:new();
				local ym = std.vector:new();	
				local lable;
				res = {};
				for _,ang in pairs(self.angs) do
					--读取数据
					local path = prename.."-"..src_type.."-ang("..ang..")"..".txt";
					if file_exists(path) then
						local str = file2StrTable( path,"\t")
						local data = file2table( path,"\t")	
						if src_type == "DIFF" then
							y,val,lable = get_bse(str,data,ang,ncol);
							xm:push_back(ang);
							 ym:push_back( (y-180+ang) * 60);
						end
						
						if src_type == "SUM" then
							y,val,lable = get_tran(str,data,ang,ncol);
							xm:push_back(ang);
							ym:push_back( val );
						end
					end
				end
				res.lable = lable;
				res.xm = xm;
				res.ym = ym;
				res.src_type = src_type;
				res.polar = polar;
				res.col = n;
				table.insert(self.read_res,res);
				 -- local view = mw:create2DView("F="..","..lable);
				 -- local gp = view:addGraph(xm,ym)
				 -- view:rescaleAxes()
			 end		 
		 end
	 end
end

function RadomeDataProc:normalise_data()
	self.nor_res = {}
	local mw = QGallop.getMainWindow();
	for _,v in pairs(self.radome_res) do
		if v.src_type == "SUM" then
			for _,vair in pairs( self.air_res ) do 
				if v.src_type == vair.src_type and 
				   v.polar == vair.polar and
				   v.col == vair.col and
				   v.lable == vair.lable then				
					res = {}
					res.lable = v.lable;
					res.xm = std.vector:new(v.xm:size());
					res.ym = std.vector:new(v.xm:size());
					res.src_type =v.src_type;
					res.polar = v.polar;
					res.col = v.col;
					for ii=0,v.xm:size()-1 do
						res.xm[ii] = v.xm[ii]
						print( ii, res.ym[ii], res.src_type, std.interpolation( vair.xm, vair.ym, v.xm[ii] ) )
						res.ym[ii] = v.ym[ii] / std.interpolation( vair.xm, vair.ym, v.xm[ii] )
						res.ym[ii] = res.ym[ii] *res.ym[ii] * 100;
					end
					table.insert(self.nor_res,res);
				end
			end
		end
		
		if v.src_type == "DIFF" then
			for _,vair in pairs( self.air_res ) do 
				if v.src_type == vair.src_type and 
				   v.polar == vair.polar and
				   v.col == vair.col and
				   v.lable == vair.lable then				
					res = {}
					res.lable = v.lable;
					res.xm = v.xm;
					res.ym = v.ym;
					res.src_type =v.src_type;
					res.polar = v.polar;
					res.col = v.col;
					table.insert(self.nor_res,res);
				end
			end
		end
	end
end

function RadomeDataProc:plot_data()
	local mw = QGallop.getMainWindow();
	for _,v in pairs(self.radome_res) do
		local view = mw:create2DView("F="..","..v.lable..v.src_type);
		local gp = view:addGraph(v.xm,v.ym)
		view:rescaleAxes()
	end
end

function RadomeDataProc:plot_data_with_filter( filter )
	local mw = QGallop.getMainWindow();
	local title="";
	if filter.src_type~=nil then
		title = title .. filter.src_type;
	end
	if filter.polar~=nil then
		title = title .. "-" .. filter.polar;
	end	
	local view = mw:create2DView(title);
	
	set_axis({axis=view:xAxis(), name="扫描角 (°)", linewidth=3,fontsize=16, tickfontsize=12} )
	set_axis({axis=view:yAxis(),  linewidth=3,fontsize=16, tickfontsize=12} )
	set_axis({axis=view:xAxis2(),  linewidth=3,fontsize=16, tickfontsize=12} )
	set_axis({axis=view:yAxis2(),  linewidth=3,fontsize=16, tickfontsize=12} )
	if filter.src_type == "SUM" then
		set_axis({axis=view:yAxis(), name="透过率 (%)"} )
	end
	if filter.src_type == "DIFF" then
		set_axis({axis=view:yAxis(), name="瞄准误差 (分)"} )
	end
	
	for _,v in pairs(self.radome_res) do
		print( v.src_type, v.polar )
		if v.src_type == filter.src_type and v.polar == filter.polar then
			print( "add:", v.src_type, v.polar )
			local gp = view:addGraph(v.xm,v.ym)
			set_graph({ gp = gp, name=v.lable })	
			if self.names[v.col] ~= nil then
				set_graph({ gp = gp, name=self.names[v.col]  })	
			end
		end
	end
	view:rescaleAxes()
end

function RadomeDataProc:plot_nor_data_with_filter( filter )
	local mw = QGallop.getMainWindow();
	local title="Nor-";
	if filter.src_type~=nil then
		title = title .. filter.src_type;
	end
	if filter.polar~=nil then
		title = title .. "-" .. filter.polar;
	end	
	local view = mw:create2DView(title);
	set_axis({axis=view:xAxis(), name="扫描角 (°)", linewidth=3,fontsize=16, tickfontsize=12} )
	set_axis({axis=view:yAxis(),  linewidth=3,fontsize=16, tickfontsize=12} )
	set_axis({axis=view:xAxis2(),  linewidth=3,fontsize=16, tickfontsize=12} )
	set_axis({axis=view:yAxis2(),  linewidth=3,fontsize=16, tickfontsize=12} )
	set_view( {view=view, legendfontsize=12} )
	if filter.src_type == "SUM" then
		set_axis({axis=view:yAxis(), name="透过率 (%)"} )
	end
	if filter.src_type == "DIFF" then
		set_axis({axis=view:yAxis(), name="瞄准误差 (分)"} )
	end
	for _,v in pairs(self.nor_res) do
		print( v.src_type, v.polar )
		if v.src_type == filter.src_type and v.polar == filter.polar then
			print( "add:", v.src_type, v.polar )
			local gp = view:addGraph(v.xm,v.ym)
			set_graph({ gp = gp, name=v.lable..v.col })	
			if self.names[v.col] ~= nil then
				set_graph({ gp = gp, name=self.names[v.col]  })	
			end
		end
	end
	view:rescaleAxes()
	set_axis({axis=view:yAxis(), range={0,100}} )
	set_axis({axis=view:yAxis2(), range={0,100}} )
end
